package cn.itcast.bos.web.action;

import javax.ws.rs.core.MediaType;

import org.apache.cxf.jaxrs.client.WebClient;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Actions;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import cn.itcast.bos.constant.Constants;
import cn.itcast.bos.domain.base.Area;
import cn.itcast.bos.domain.take_delivery.Order;
import cn.itcast.bos.web.action.common.BaseAction;
import cn.itcast.crm.domain.Customer;

@ParentPackage("json-default")
@Namespace("/")
@Controller
@Actions
@Scope("prototype")
public class OrderAction extends BaseAction<Order> {
	
	private String sendAreaInfo;//发件人省市县信息
	private String recAreaInfo;//收件人省市县信息
	
	public void setSendAreaInfo(String sendAreaInfo) {
		this.sendAreaInfo = sendAreaInfo;
	}

	public void setRecAreaInfo(String recAreaInfo) {
		this.recAreaInfo = recAreaInfo;
	}

	@Action(value = "order_add", results = { @Result(name = "success", type = "redirect", location = "index.html") })
	public String add() {
		//手动封装收件人和寄件人省市区信息
		Area sendArea = new Area();
		String[] sendAreaArr = sendAreaInfo.split("/");
		sendArea.setProvince(sendAreaArr[0]);
		sendArea.setCity(sendAreaArr[1]);
		sendArea.setDistrict(sendAreaArr[2]);
		Area recArea = new Area();
		String[] recAreaArr = recAreaInfo.split("/");
		recArea.setProvince(recAreaArr[0]);
		recArea.setCity(recAreaArr[1]);
		recArea.setDistrict(recAreaArr[2]);
		model.setSendArea(sendArea);
		model.setRecArea(recArea);
		//关联当前登录的客户
		Customer  customer = (Customer) ServletActionContext.getRequest().getSession().getAttribute("customer");
		model.setCustomer_id(customer.getId());
		//调用bos_management系统
		WebClient.create(Constants.BOS_MANAGEMENT_URL+"/services/orderService/order")
				.type(MediaType.APPLICATION_JSON)
				.post(model);
		return SUCCESS;
	}
}
